package com.firesrc.service;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public abstract class SuperScheduledExecutorManager {

  protected ScheduledExecutorService executors;

  public synchronized void stop() {
    if (executors != null && !executors.isShutdown()) {
      executors.shutdownNow();
      executors = null;
    }
  }

  public boolean isShutdown() {
    if (executors == null || executors.isShutdown()) {
      return true;
    }
    return false;
  }

  public synchronized void start() {
    if (executors == null) {
      executors = Executors.newScheduledThreadPool(getThreadCount());
    }
    Runnable task = getSubRunnableTask();
    executors.scheduleWithFixedDelay(task, 5, getScheduledInterval() * 60, TimeUnit.SECONDS);
  }

  protected abstract long getScheduledInterval();

  protected abstract Runnable getSubRunnableTask();

  protected abstract int getThreadCount();

}